package com.nextive.weightlogger;

import com.nextive.weightlogger.activity.LogWeightActivity;
import com.nextive.weightlogger.activity.SettingsActivity;
import com.nextive.weightlogger.activity.ShareActivity;
import com.nextive.weightlogger.activity.WeightHistoryActivity;

import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

/**
 * Manages the options menu of the activities in this application.
 * 
 * Populates the menu items and handles menu item clicks launching activity
 * associated to it.
 * 
 * This class should be instantiated in the managed activity's onCreate method
 * and it should invoke all of this classes' public methods in its so named
 * methods.
 * 
 * @author Federico Baseggio <fedebaseggio@gmail.com>
 */
public class MenuManager {
	/**
	 * The activity which options menu is to be managed by this menu manager.
	 */
	private Activity activity;
	
	/**
	 * Constructor.
	 * 
	 * @param activity
	 *            The activity which options menu is to be managed by this menu
	 *            manager.
	 */
	public MenuManager(Activity activity)
	{
		this.activity = activity;
	}

	/**
	 * Method to be invoked in the onCreateOptionsMenu method of the activity
	 * whose menu is managed.
	 * 
	 * @param menu
	 *            The menu parameter of the onCreateOptionsMenu method of the
	 *            managed activity.
	 * @return Value to be returned by the onCreateOptionsMenu method of the
	 *         managed activity.
	 */
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = activity.getMenuInflater();
	    inflater.inflate(R.menu.main, menu);
	    return true;
	}

	/**
	 * Method to be invoked in the onOptionsItemSelected method of the activity
	 * whose menu is managed.
	 * 
	 * @param item
	 *            The item parameter of the onOptionsItemSelected method of the
	 *            managed activity.
	 * @return Value to be returned by the onOptionsItemSelected method of the
	 *         managed activity.
	 */
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId())
		{
		case R.id.menu_item_settings:
			activity.startActivity(new Intent(activity, SettingsActivity.class));
			return true;
		case R.id.menu_item_log_weight:
			activity.startActivity(new Intent(activity, LogWeightActivity.class));
			return true;
		case R.id.menu_item_history:
			activity.startActivity(new Intent(activity, WeightHistoryActivity.class));
			return true;
		case R.id.menu_item_share:
			activity.startActivity(new Intent(activity, ShareActivity.class));
			return true;
		}
		return false;
	}
}
